﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;

public partial class BaseManage_EditPortSetting : Frame.Pages.BasePage
{
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        if (!IsPostBack && !IsCallback)
        {
            //构建LTS数据上下文
            using (DAL.CMSDALDataContext Dcontext = new DAL.CMSDALDataContext())
            {
                BI_SysSettings sysSetting = Dcontext.BI_SysSettings.FirstOrDefault(o => o.SettingName == "PortAddress");
                if (sysSetting == null)
                {
                    sysSetting = new BI_SysSettings()
                    {
                        SettingName = "PortAddress",
                        Value = "9527",
                        Remark = "数据端口地址"
                    };
                    Dcontext.BI_SysSettings.InsertOnSubmit(sysSetting);
                    Dcontext.SubmitChanges();
                }
                tbxAddress.Text = sysSetting.Value;
                sysSetting = Dcontext.BI_SysSettings.FirstOrDefault(o => o.SettingName == "PortNumber");
                if (sysSetting == null)
                {
                    sysSetting = new BI_SysSettings()
                    {
                        SettingName = "PortNumber",
                        Value = "9527",
                        Remark = "数据端口"
                    };
                    Dcontext.BI_SysSettings.InsertOnSubmit(sysSetting);
                    Dcontext.SubmitChanges();
                }
                tbxPort.Text = sysSetting.Value;
                sysSetting = Dcontext.BI_SysSettings.FirstOrDefault(o => o.SettingName == "PortUserName");
                if (sysSetting == null)
                {
                    sysSetting = new BI_SysSettings()
                    {
                        SettingName = "PortUserName",
                        Value = "felix",
                        Remark = "数据端口用户"
                    };
                    Dcontext.BI_SysSettings.InsertOnSubmit(sysSetting);
                    Dcontext.SubmitChanges();
                }
                tbxUser.Text = sysSetting.Value;
                sysSetting = Dcontext.BI_SysSettings.FirstOrDefault(o => o.SettingName == "PortUserPWD");
                if (sysSetting == null)
                {
                    sysSetting = new BI_SysSettings()
                    {
                        SettingName = "PortUserPWD",
                        Value = "",
                        Remark = "数据端口用户密码"
                    };
                    Dcontext.BI_SysSettings.InsertOnSubmit(sysSetting);
                    Dcontext.SubmitChanges();
                }
                tbxPwd.Text = sysSetting.Value;
                
            }
        }

    }
    protected void ASPxButton1_Click(object sender, EventArgs e)
    {

        //构建LTS数据上下文
        using (DAL.CMSDALDataContext Dcontext = new DAL.CMSDALDataContext())
        {
            BI_SysSettings sysSetting = Dcontext.BI_SysSettings.FirstOrDefault(o => o.SettingName == "PortAddress");
            if (sysSetting == null)
            {
                sysSetting = new BI_SysSettings()
                {
                    SettingName = "PortAddress",
                    Value = tbxAddress.Text,
                    Remark = "数据端口地址"
                };
                Dcontext.BI_SysSettings.InsertOnSubmit(sysSetting);
            }
            else
            {
                sysSetting.Value = tbxAddress.Text;
            }
            sysSetting = Dcontext.BI_SysSettings.FirstOrDefault(o => o.SettingName == "PortNumber");
            if (sysSetting == null)
            {
                sysSetting = new BI_SysSettings()
                {
                    SettingName = "PortNumber",
                    Value = tbxPort.Text,
                    Remark = "数据端口"
                };
                Dcontext.BI_SysSettings.InsertOnSubmit(sysSetting);
            }
            else
            {
                sysSetting.Value = tbxPort.Text;
            }
            sysSetting = Dcontext.BI_SysSettings.FirstOrDefault(o => o.SettingName == "PortUserName");
            if (sysSetting == null)
            {
                sysSetting = new BI_SysSettings()
                {
                    SettingName = "PortUserName",
                    Value = tbxUser.Text,
                    Remark = "数据端口用户"
                };
                Dcontext.BI_SysSettings.InsertOnSubmit(sysSetting);
            }
            else
            {
                sysSetting.Value = tbxUser.Text;
            }
            sysSetting = Dcontext.BI_SysSettings.FirstOrDefault(o => o.SettingName == "PortUserPWD");
            if (sysSetting == null)
            {
                sysSetting = new BI_SysSettings()
                {
                    SettingName = "PortUserPWD",
                    Value = tbxPwd.Text,
                    Remark = "数据端口用户密码"
                };
                Dcontext.BI_SysSettings.InsertOnSubmit(sysSetting);
            }
            else
            {
                sysSetting.Value = tbxPwd.Text;
            }
            Dcontext.SubmitChanges();
        }
        Frame.Global.GlobalFunctions.Alert("提交修改成功！");
    }
    

    protected override string ActionID
    {
        get { return "0"; }
    }
}